"""
Can we please get the panel object to see its members?
"""
import wx
from peak.events import trellis
from wxpita import *
import wxpita.databind

update_index = 0
tasks = ['Search & Destroy', 'Assassination', 'Infiltration',
         'Agent Extraction', 'Document Retrieval', 'Donut Run']
statuses = ['Active', 'Inactive', 'Deceased', 'Gone Fishing']

class Agent(trellis.Component):
    trellis.values(
        codename = '',
        taskids = [],
        tasks = [],
        statusid=0,
        status = '',
        clearance = 1,
    )
    trellis.rules(
        tasks = lambda self: [tasks[i] for i in self.taskids],
        status = lambda self: statuses[self.statusid]
    )

    @trellis.action
    def show(self):
        global update_index
        update_index += 1
        print '%d. %s' % (update_index, self)
        print '-'*80
        
    def __str__(self):
        return '<%s, %s, %s, [%s]>' % (self.codename, self.status,
                                       self.clearance, ', '.join(self.tasks))

agent = Agent()

f = SizedFrame(sizerType='horizontal') [
    ListBox(name='lbox', expand=True, choices=['Freya', 'Hermes', 'Jupiter']),

    SizedPanel(name='agentform', sizerType='form', expand=True) [
        StaticText(label='Code Name'),
        TextCtrl(name='codename', expand=True),

        StaticText(label='Status'),
        RadioBox(name='statusid', choices=statuses,
                 style='ra_specify_rows', majorDimension=4),

        StaticText(label='Security Clearance'),
        Slider(name='clearance', minValue=1, maxValue=10, expand=True,
               style='sl_autoticks|sl_labels'),

        StaticText(label='Tasks'),
        ListBox(name='taskids', choices=tasks, expand=True, style='lb_multiple'),
    ],
]

@f.Init
def _():
    f.clearance.SetValue(1)
    wxpita.databind.DataBind(agent, f.agentform,
        'codename, taskids:multi, statusid, clearance')

@f.lbox.listbox_selected
@trellis.modifier               # to prevent 4 prints in a row
def _(evt):
    index = evt.EventObject.GetSelection()
    a = agent

    if index == 0:
        a.codename, a.taskids, a.statusid, a.clearance = \
            'Freya', [1, 3], 1, 4
    elif index == 1:
        a.codename, a.taskids, a.statusid, a.clearance = \
            'Hermes', [2, 4], 3, 7
    elif index == 2:
        a.codename, a.taskids, a.statusid, a.clearance = \
            'Jupiter', [0, 1, 2, 5], 2, 10
    
f.Show('Data Binding Example', size='fit')
